#!/usr/bin/perl -w

use strict;
use warnings;
use WWW::Google::Cloud::Messaging;

my $api_key = 'AIzaSyA6X3oPhBN7j75vt7k-vK2dCX3jHlB_pIE';
my $gcm = WWW::Google::Cloud::Messaging->new(api_key => $api_key);
my $cat = "bel";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
						localtime(time);
$year += "1900";

my $date = "$year$mon$mday";
my $time = "$hour$min$sec";

print "Say something: ";
my $msg = <>;

my $string = "$date;$time;$cat;$msg";

my $res = $gcm->send({
	registration_ids => [ 'APA91bF-KE92K2f6S6aI9vSpPHtVhFW4k0wsptSjIJOglChUA3cHjxd_njYHdWsNodAKQV8EzLNbVfYN63dt_i5fxPuARmySUKiPQnx7YmQdSMIc_ltm99yAxOvoM3vNVt3QorJOi7IACLNXsvw38fCdkYXqHnBysg' ],
	time_to_live =>	 0,
	data             => {
	message => $string
	}
});

exit 0;
die $res->error unless $res->is_success;

my $results = $res->results;
while (my $result = $results->next) {
	my $reg_id = $result->target_reg_id;
	if ($result->is_success) {
		printf "message_id: %s\nreg_id: %s\n\n", $result->message_id, $reg_id;
	}
	else {
		printf "error: %s\nreg_id: %s\n\n", $result->error, $reg_id;
	}

	if ($result->has_canonical_id) {
		printf "reg_id %s is old!\nrefreshed reg_id is %s\n\n", $reg_id, $result->registration_id;
	}
}
